from urllib.parse import urlencode
import re, os, sys, json
import requests
from urllib3 import encode_multipart_formdata
import time


class ATS():
    def __init__(self, username, pwd):
        self.login(username, pwd)

    def login(self, username, pwd):
        pass

    def create_new_case(self, filepath):
        # Content-Type 表示 body 的数据格式, multipart/form-data,通常用于 HTML 提交图片或者文件
        with open(filepath, mode="rb") as f1:
            fb1 = f1.read()
        with open(filepath, mode="rb") as f2:
            fb2 = f2.read()
        pyname = "filename"
        pngname = "filename"

        files_data = {"calssify": "mokuai",
                      "caseid": "caseid",
                      "pyfile": (pyname, fb1),
                      "expectfile": (pngname, fb2)
                      }
        encode_data = encode_multipart_formdata(files_data)

        #  如果encode_data格式不对，还可自行处理，比如下面增加第二张图片
        file_data = encode_data[0]
        boundary = encode_data[1].split("boundary=")[1]
        plus_bytes = """ ****** """
        imgfile = "deyt33"
        with open(imgfile, mode="rb") as f:
            fb3 = f.read()
            demo = """ ****** %s """ % (fb3)
        plus_byte = demo + plus_bytes

        files_data_new = file_data + plus_byte.encode("utf-8")
        headers = {"Content-Type": encode_data[1]}
        con = requests.post(url="", data=files_data_new, headers=headers)
